"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = require("react");
var _useRealtimeRef = _interopRequireDefault(require("../useRealtimeRef"));
/**
 * 管理模态框显示隐藏状态，及模态框表单数据回填
 * @param options options.form ant design 的 FormInstance 示例，可操纵表单
 * @param options options.getValuesFromDataSource 从 dataSource 中获取表单展示数据
 * @param options options.onSubmit 点击确认后调用
 * @returns visible 模态框显示隐藏状态
 * @returns dataSource 模态框展示实体数据
 * @returns loading 是否提交过程中
 * @returns show 显示模态框，条件设置模态框展示实体数据
 * @returns hide 隐藏模态框
 * @returns submit 提交
 * @returns form ant design 的 FormInstance 示例，可操纵表单
 * @returns props 模态框、抽屉渲染时常用 props 聚合
 */
var useModal = function useModal(options) {
  var _ref = options || {},
    form = _ref.form,
    format = _ref.format,
    onSubmit = _ref.onSubmit;
  // 实时缓存最新的销毁函数
  var onSubmitRef = (0, _useRealtimeRef.default)(onSubmit);
  var _useState = (0, _react.useState)(false),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    visible = _useState2[0],
    setVisible = _useState2[1];
  var _useState3 = (0, _react.useState)(undefined),
    _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
    dataSource = _useState4[0],
    setDataSource = _useState4[1];
  var _useState5 = (0, _react.useState)(false),
    _useState6 = (0, _slicedToArray2.default)(_useState5, 2),
    loading = _useState6[0],
    setLoading = _useState6[1];
  var show = (0, _react.useCallback)(function (record) {
    if (record) setDataSource(record);
    setVisible(true);
  }, []);
  var hide = (0, _react.useCallback)(function () {
    setVisible(false);
    setDataSource(undefined);
  }, []);
  var submit = (0, _react.useCallback)(function (vals) {
    return new Promise(function (resolve, reject) {
      if (onSubmitRef.current) {
        setLoading(true);
        onSubmitRef.current(vals, dataSource).then(function (res) {
          setLoading(false);
          if (res) {
            // { success } 格式自动隐藏
            if (res.success) hide();
            resolve(res);
          } else {
            setLoading(false);
            reject();
          }
        }).catch(function () {
          setLoading(false);
          reject();
        });
      }
    });
  }, [dataSource]);
  var props = (0, _react.useMemo)(function () {
    return {
      visible: visible,
      dataSource: dataSource,
      loading: loading,
      onCancel: hide,
      onOk: submit
    };
  }, [visible, dataSource, loading, hide, submit]);
  (0, _react.useEffect)(function () {
    if (!form) return;
    if (visible) {
      if (dataSource) {
        var values = format ? format(dataSource) : dataSource;
        form.setFieldsValue(values);
      }
    } else {
      form.resetFields();
    }
  }, [form, visible]);
  return {
    visible: visible,
    dataSource: dataSource,
    loading: loading,
    show: show,
    hide: hide,
    submit: submit,
    form: form,
    props: props
  };
};
var _default = useModal;
exports.default = _default;